package com.framework.common.utils;

import java.util.HashMap;
import java.util.Map;

/**
 * 返回数据
 */
public class R extends HashMap<String, Object> {
	private static final long serialVersionUID = 1L;

	public R() {

		put("code", 1);
		put("msg", "success");
		//put("ext","");
	}

	public static R error() {
		return response(ResponseCode.FAIL);
	}

	public static R error(String msg) {
		return response(ResponseCode.FAIL, msg);
	}

	public static R error(int code, String msg) {
		R r = new R();
		r.put("code", code);
		r.put("msg", msg);
		return r;
	}

	public static R ok(String msg) {
		R r = new R();
		r.put("msg", msg);
		return r;
	}

	public static R ok(Integer code, String msg) {
		R r = new R();
		r.put("msg", msg);
		r.put("code", code);
		return r;
	}

	public static R ok(Map<String, Object> map) {
		R r = new R();
		r.putAll(map);
		return r;
	}

	public static R ok(Object object) {
		R r = new R();
		r.put("data", object);
		return r;
	}

	public static R ok() {
		return new R();
	}

	/**
	 * 根据状态码返回信息
	 *
	 * @param code
	 * @return
	 */
	public static R response(ResponseCode code) {
		R r = new R();
		r.put("code", code.getCode());
		r.put("msg", code.getDesc());
		return r;
	}

	/**
	 * 会覆盖 ResponseCode 的 desc 信息
	 *
	 * @param code 提示码
	 * @param msg  消息
	 * @return
	 */
	public static R response(ResponseCode code, String msg) {
		R r = new R();
		r.put("code", code.getCode());
		r.put("msg", msg);
		return r;
	}

	@Override
	public R put(String key, Object value) {
		super.put(key, value);
		return this;
	}
}
